/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * <p>Project: javase - Img1
 * <p>Powered by webrx On 2021-12-17 09:16:51
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Img1 {
    public static void main(String[] args) {
        var i = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
        var g = i.createGraphics();
        //g.setColor(Color.BLUE);
        var rand = new Random();
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
        g.fillRect(0, 0, 300, 300);

        //g.setColor(Color.RED);
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
        g.setStroke(new BasicStroke(1));
        g.drawLine(0, 0, 300, 300);

        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
        g.setStroke(new BasicStroke(1));

        g.drawLine(0, 150, 300, 150);

        g.setStroke(new BasicStroke(5));
        g.setColor(Color.YELLOW);
        g.fillRect(30, 30, 130, 130);

        g.drawRect(180, 180, 80, 80);

        g.dispose();

        try {
            //将图片文件保存到硬盘
            ImageIO.write(i, "png", new File("c:/temp.png"));
            //调用系统命令打开这个图片
            Runtime.getRuntime().exec("cmd /k start c:/temp.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
